<template>
    <van-button :icon="value ? 'star' : 'star-o'" :class="{ collected: value }" @click="onCollect" :loading="loading">
    </van-button>
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article.js'

export default {
  name: 'CollectArticle',
  data() {
    return {
      loading: false,
    }
  },
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      required:true,
    }
  },
  methods: {
    async onCollect() {
      this.loading = true
      try {
        if(this.value) {
          await deleteCollect(this.articleId)
        } else {
          await addCollect(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast.fail('操作失败')
      }
      this.loading = false
    }
  },
  created() {

  }
}
</script>

<style scoped>
  .collected .van-icon {
      color: #FFB5C5
  }

</style>